<?php
/**
 * Created by PhpStorm.
 * User: Mak
 * Date: 2016/4/14 0014
 * Time: 22:12
 */

namespace Admin\Controller;

use Think\Controller;

/**
 * Class MatchDetailsController
 * @package Admin\Controller
 */
class MatchDetailsController extends Controller
{

    public function index()
    {
        $this->display();
    }

    public function one()
    {
        $enid = I("get.enid");
        $StuEnroll = D("StuEnroll");
        $ret = $StuEnroll->where("enid={$enid}")->order("addtime")->select();
        $this->assign("ret", $ret)->assign("enid", $enid);
        $this->display();

    }

    public function team()
    {
        $enid = I("get.enid");
        $TeamInfo = D("Team");
        $ret = $TeamInfo->where("enid=$enid")->order("addtime")->select();
        $this->assign("ret", $ret)->assign("enid", $enid);
        $this->display();
    }

    public function outputTeam()
    {
        $enid = I("get.enid");
        if (!empty($enid)) {
            import("Org.Util.PHPExcel");
            $Team = D("Team");
            $TeamInfo = D("TeamInfo");
            $ret = $Team->where("enid={$enid}")->order("addtime")->select();
            $PHPExcel = new \PHPExcel();
            $Sheet = $PHPExcel->getActiveSheet();
            $Sheet->getDefaultStyle()->getFont()->setName("宋体")->setSize(14);
            $Sheet->getDefaultStyle()
                ->getAlignment()
                ->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER)
                ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $Sheet->getColumnDimension('A')->setWidth(5);
            $Sheet->getColumnDimension('B')->setAutoSize(true);
            $Sheet->getColumnDimension('C')->setWidth(20);
            $Sheet->getColumnDimension('D')->setWidth(30);
            $Sheet->getColumnDimension('E')->setWidth(6);
            $Sheet->getColumnDimension('F')->setWidth(30);
            $Sheet->getColumnDimension('G')->setAutoSize(true);
            $Sheet->getColumnDimension('H')->setWidth(50);
            $Sheet->getColumnDimension('I')->setAutoSize(true);
            $Sheet->getColumnDimension('J')->setAutoSize(true);
            $Sheet->getColumnDimension('K')->setAutoSize(true);
            $Sheet->getColumnDimension('L')->setAutoSize(true);
            $Sheet->getColumnDimension('M')->setAutoSize(true);
            $Sheet->getColumnDimension('N')->setAutoSize(true);
            $Sheet->getColumnDimension('O')->setAutoSize(true);
            $Sheet->getColumnDimension('P')->setAutoSize(true);
            $Sheet->getColumnDimension('Q')->setAutoSize(true);
            $Sheet->getColumnDimension('R')->setAutoSize(true);
            $Sheet->getColumnDimension('S')->setAutoSize(true);
            $Sheet->getColumnDimension('T')->setAutoSize(true);
            $Sheet->getColumnDimension('U')->setAutoSize(true);
            $Sheet->getColumnDimension('V')->setAutoSize(true);
            $Sheet->getColumnDimension('W')->setAutoSize(true);
            $Sheet->getColumnDimension('X')->setAutoSize(true);
            $Sheet->getColumnDimension('Y')->setAutoSize(true);
            $Sheet->getColumnDimension('Z')->setAutoSize(true);
            $Sheet->getColumnDimension('AA')->setAutoSize(true);
            $Sheet->getColumnDimension('AB')->setAutoSize(true);
            $Sheet->getColumnDimension('AC')->setAutoSize(true);
            $Sheet->getColumnDimension('AD')->setAutoSize(true);
            $Sheet->setCellValue("A1", "序号")
                ->setCellValue("B1", "学号(队长)")
                ->setCellValue("C1", "系部(队长)")
                ->setCellValue("D1", "班级(队长)")
                ->setCellValue("E1", "性别(队长)")
                ->setCellValue("F1", "姓名(队长)")
                ->setCellValue("G1", "学号1")
                ->setCellValue("H1", "系部1")
                ->setCellValue("I1", "班级1")
                ->setCellValue("J1", "性别1")
                ->setCellValue("K1", "姓名1")
                ->setCellValue("L1", "学号2")
                ->setCellValue("M1", "系部2")
                ->setCellValue("N1", "班级2")
                ->setCellValue("O1", "性别2")
                ->setCellValue("P1", "姓名2")
                ->setCellValue("Q1", "学号3")
                ->setCellValue("R1", "系部3")
                ->setCellValue("S1", "班级3")
                ->setCellValue("T1", "性别3")
                ->setCellValue("U1", "姓名3")
                ->setCellValue("V1", "学号4")
                ->setCellValue("W1", "系部4")
                ->setCellValue("X1", "班级4")
                ->setCellValue("Y1", "性别4")
                ->setCellValue("Z1", "姓名4")
                ->setCellValue("AA1", "赛项编号")
                ->setCellValue("AB1", "团队编号")
                ->setCellValue("AC1", "赛项名称")
                ->setCellValue("AD1", "报名时间")
                ->setCellValue("AE1", "成绩")
                ->getStyle("A1:AE1")
                ->getFont()
                ->setName("微软雅黑")
                ->setSize(14)
                ->setBold(true);

            $startcell = 2;
            $G = "G";
            $H = "H";
            $I = "I";
            $J = "J";
            $K = "K";
            foreach ($ret as $rows) {
                $res = $TeamInfo->where("teamid={$rows['teamid']}")->select();
                $i = 1;
                $Sheet->getStyle("B" . $startcell)->getNumberFormat()->setFormatCode(\PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                $Sheet->getStyle("G" . $startcell)->getNumberFormat()->setFormatCode(\PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                $Sheet->getStyle("I" . $startcell)->getNumberFormat()->setFormatCode(\PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                $Sheet->getStyle("J" . $startcell)->getNumberFormat()->setFormatCode(\PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                $Sheet->setCellValue("A" . $startcell, $i++)
                    ->setCellValueExplicit("AA" . $startcell, $rows["enid"], \PHPExcel_Cell_DataType::TYPE_STRING)
                    ->setCellValueExplicit("AB" . $startcell, $rows["teamid"], \PHPExcel_Cell_DataType::TYPE_STRING)
                    ->setCellValue("AC" . $startcell, getEnrName($rows["enid"]))
                    ->setCellValue("AD" . $startcell, date("Y-m-d H:i:s", $rows["addtime"]))
                    ->setCellValueExplicit("AE" . $startcell, $rows["grade"], \PHPExcel_Cell_DataType::TYPE_STRING);
                foreach ($res as $v) {
                    if ($v['captain']) {
                        $Sheet->setCellValueExplicit("B" . $startcell, $v["stuid"], \PHPExcel_Cell_DataType::TYPE_STRING)
                            ->setCellValue("C" . $startcell, StuIdToDept($v["stuid"]))
                            ->setCellValue("D" . $startcell, StuIdToClass($v["stuid"]))
                            ->setCellValue("E" . $startcell, StuIdToSex($v["stuid"]))
                            ->setCellValue("F" . $startcell, getStuName($v["stuid"]));
                    } else {
                        $Sheet->setCellValueExplicit($G . $startcell, $v["stuid"], \PHPExcel_Cell_DataType::TYPE_STRING)
                            ->setCellValue($H . $startcell, StuIdToDept($v["stuid"]))
                            ->setCellValue($I . $startcell, StuIdToClass($v["stuid"]))
                            ->setCellValue($J . $startcell, StuIdToSex($v["stuid"]))
                            ->setCellValue($K . $startcell, getStuName($v["stuid"]));
                        $G = chr(ord($G) + 5);
                        $H = chr(ord($H) + 5);
                        $I = chr(ord($I) + 5);
                        $J = chr(ord($J) + 5);
                        $K = chr(ord($K) + 5);
                    }
                }

                $startcell++;
            }

            $obj = \PHPExcel_IOFactory::createWriter($PHPExcel, "Excel5");
            browser_export("[" . getEnrName($enid) . "]报名信息表");
            $obj->save("php://output");
        } else {
            $this->erroer("导出有误，请重试！");
        }

    }

    /**
     * @throws \PHPExcel_Reader_Exception
     * TODO
     */
    public function outputOne()
    {
        $enid = I("get.enid");
        if (!empty($enid)) {
            import("Org.Util.PHPExcel");
            $StuEnroll = D("StuEnroll");
            $ret = $StuEnroll->where("enid={$enid}")->order("addtime")->select();
            $PHPExcel = new \PHPExcel();
            $Sheet = $PHPExcel->getActiveSheet();
            $Sheet->getDefaultStyle()->getFont()->setName("宋体")->setSize(14);
            $Sheet->getDefaultStyle()
                ->getAlignment()
                ->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER)
                ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $Sheet->getColumnDimension('A')->setWidth(5);
            $Sheet->getColumnDimension('B')->setAutoSize(true);
            $Sheet->getColumnDimension('C')->setWidth(20);
            $Sheet->getColumnDimension('D')->setWidth(30);
            $Sheet->getColumnDimension('E')->setWidth(6);
            $Sheet->getColumnDimension('F')->setWidth(30);
            $Sheet->getColumnDimension('G')->setAutoSize(true);
            $Sheet->getColumnDimension('H')->setWidth(50);
            $Sheet->getColumnDimension('I')->setAutoSize(true);
            $Sheet->getColumnDimension('J')->setAutoSize(true);
            $Sheet->setCellValue("A1", "序号")
                ->setCellValue("B1", "学号")
                ->setCellValue("C1", "系部名称")
                ->setCellValue("D1", "班级")
                ->setCellValue("E1", "性别")
                ->setCellValue("F1", "姓名")
                ->setCellValue("G1", "赛项编号")
                ->setCellValue("H1", "赛项名称")
                ->setCellValue("I1", "报名时间")
                ->setCellValue("J1", "成绩")
                ->getStyle("A1:J1")
                ->getFont()
                ->setName("微软雅黑")
                ->setSize(14)
                ->setBold(true);
            $startcell = 2;
            $i = 1;
            foreach ($ret as $rows) {
                $Sheet->getStyle("B" . $startcell)->getNumberFormat()->setFormatCode(\PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                $Sheet->getStyle("G" . $startcell)->getNumberFormat()->setFormatCode(\PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                $Sheet->getStyle("I" . $startcell)->getNumberFormat()->setFormatCode(\PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                $Sheet->getStyle("J" . $startcell)->getNumberFormat()->setFormatCode(\PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                $Sheet->setCellValue("A" . $startcell, $i++)
                    ->setCellValueExplicit("B" . $startcell, $rows["stuid"], \PHPExcel_Cell_DataType::TYPE_STRING)
                    ->setCellValue("C" . $startcell, StuIdToDept($rows["stuid"]))
                    ->setCellValue("D" . $startcell, StuIdToClass($rows["stuid"]))
                    ->setCellValue("E" . $startcell, StuIdToSex($rows["stuid"]))
                    ->setCellValue("F" . $startcell, getStuName($rows["stuid"]))
                    ->setCellValueExplicit("G" . $startcell, $rows["enid"], \PHPExcel_Cell_DataType::TYPE_STRING)
                    ->setCellValue("H" . $startcell, getEnrName($rows["enid"]))
                    ->setCellValueExplicit("I" . $startcell, date("Y-m-d H:i:s", $rows["addtime"]), \PHPExcel_Cell_DataType::TYPE_STRING)
                    ->setCellValueExplicit("J" . $startcell, StuIdToSex($rows["grade"]), \PHPExcel_Cell_DataType::TYPE_STRING);
                $startcell++;
            }

            $obj = \PHPExcel_IOFactory::createWriter($PHPExcel, "Excel5");
            browser_export("[" . getEnrName($enid) . "]报名信息表");
            $obj->save("php://output");
        } else {
            $this->erroer("导出有误，请重试！");
        }

    }

    public function importOneScore()
    {
        import("Org.Util.PHPExcel");
        $StuEnroll = D("StuEnroll");
        $cnt = 0;
        $upload = use_upload();
        $file = "./UPLOADS/" . $upload["score"]["savepath"] . $upload["score"]["savename"];
        $fileType = \PHPExcel_IOFactory::identify($file);
        $Reader = \PHPExcel_IOFactory::createReader($fileType);
        $PHPExcel = $Reader->load($file);
        $data = $PHPExcel->getSheet(0)->toArray();
//        dump($data);
        foreach ($data as $k => $row) {
            if ($k == 0) {
                continue;
            }
            $save["grade"] = $row[9];
            $where["stuid"] = $row[1];
            $where["enid"] = $row[6];
            $ret = $StuEnroll->where($where)->save($save);
            if ($ret) {
                $cnt += 1;
            }
        }
        $this->success("有" . $cnt . "条数据导入成功");
    }

    /**
     * @throws \PHPExcel_Reader_Exception
     */
    public function importTeamScore()
    {
        import("Org.Util.PHPExcel");
        $Team = D("Team");
        $cnt = 0;
        $upload = use_upload();
        $file = "./UPLOADS/" . $upload["score"]["savepath"] . $upload["score"]["savename"];
        $fileType = \PHPExcel_IOFactory::identify($file);
        $Reader = \PHPExcel_IOFactory::createReader($fileType);
        $PHPExcel = $Reader->load($file);
        $data = $PHPExcel->getSheet(0)->toArray();
//        dump($data);
        foreach ($data as $k => $row) {
            if ($k == 0) {
                continue;
            }
            $save["grade"] = $row[30];
            $where["teamid"] = $row[27];
            $ret = $Team->where($where)->save($save);
            if ($ret) {
                $cnt += 1;
            }
        }
        $this->success("有" . $cnt . "条数据导入成功");
    }

    public function oneGrade(){
        $enid = I("get.enid");
        $StuEnroll = D("StuEnroll");
        $ret = $StuEnroll->where("enid={$enid}")->order("grade desc")->select();
        $this->assign("ret", $ret)->assign("enid", $enid);

        $Grade=getGrade($StuEnroll,$enid);
        $this->assign("Grade",$Grade);
        $this->display();
    }

    public function teamGrade(){
        $enid = I("get.enid");
        $TeamInfo = D("Team");
        $ret = $TeamInfo->where("enid=$enid")->order("grade desc")->select();
        $this->assign("ret", $ret)->assign("enid", $enid);

        $Grade=getGrade($TeamInfo,$enid);
        $this->assign("Grade",$Grade);
        $this->display();
    }
}